package chuangyi.com.org.ViewModel;

import android.content.Context;

import com.android.databinding.library.baseAdapters.BR;
import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.List;

import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;
import chuangyi.com.org.DOMIHome.adapter.XListBindingAdapter;

/**
 * Created by zxh on 16/12/20.
 */

public class MyVideoModel extends XListModel {
    private boolean loading;

    public MyVideoModel(Context context) {
        super(context);
        super.getHandler().setTitle("我购买的视频");
        super.getAdapter().setVariableId(BR.video);
    }

    @Override
    public void load(final int page) {
        if (loading) {
            return;
        }
        loading = true;
        final Context context = getContext();
        final XListAdapterInterface adapter = super.getAdapter();
//        adapter.clear(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                List videoList = WebApi.getMyVideo(context, page);
                try {
                    for (Object menu : videoList) {
                        LinkedTreeMap map = (LinkedTreeMap) menu;
                        CollectVideoItem item = new CollectVideoItem(context, R.layout.i_paid_video);

//                        item.setUid(Utils.getIntValue(map.get("uid")));
                        item.setCover((String) map.get("cover"));
//                        item.setFreeflag(Integer.valueOf((String) map.get("freeflag")));
//                        item.setNotepath((String) map.get("notepath"));
//                        item.setPrice(Double.valueOf((String) map.get("price")));
//                        item.setSpeaker((String) map.get("speaker"));
//                        item.setUploadate(Utils.getDateFromTimestamp(map.get("uploaddate")));
                        item.setCollectcoun(Utils.getIntValue(map.get("collectcoun")));
                        item.setBrowser(Utils.getIntValue(map.get("browser")));
                        item.setVideodesc((String) map.get("videodesc"));
                        item.setVideoid(Integer.valueOf((String) map.get("videoid")));
                        item.setVideoname((String) map.get("videoname"));
//                        item.setVideopath((String) map.get("videopath"));
//                        item.setVideotype((String) map.get("videotype"));
                        adapter.add(item);
                    }
                    if (videoList.size() > 0) {
                        setCurrentPage(page);
                    }
                    notifyDataSetChanged(videoList);
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                loading = false;
            }
        }).start();
    }
}
